package model

import (
	"fmt"
	"github.com/sirupsen/logrus"
)

type (
	Hosts []*Host

	Host struct {
		Model
		Name              string `json:"name" gorm:"column:name;size:255;default:'';"`
		DeviceType        string `json:"device_type" gorm:"column:device_type;size:255;default:'';"`
		Lessee            string `json:"lessee" gorm:"column:lessee;size:255;default:'';"`
		SerialNo          string `json:"serial_no" gorm:"column:serial_no;size:255;default:'';"`
		IP                string `json:"ip" gorm:"column:ip;size:255;default:'';"`
		Port              string `json:"port" gorm:"column:port;size:255;default:'';"`
		DeviceCount       string `json:"device_count" gorm:"column:device_count;size:255;default:'';"`
		Username          string `json:"username" gorm:"column:username;size:255;default:'';"`
		Password          string `json:"password" gorm:"column:password;size:255;default:'';"`
		StreamMonitorFlag string `json:"stream_monitor_flag" gorm:"column:stream_monitor_flag;size:255;default:'';"`
		F4GFlag           string `json:"f4G_flag" gorm:"column:f4G_flag;size:255;default:'';"`
		ApplianceMode     string `json:"appliance_mode" gorm:"appliance_mode;size:255;default:'';"`
		ApplianceUptime   string `json:"appliance_uptime" gorm:"appliance_uptime;size:255;default:'';"`
		ServiceUptime     string `json:"service_uptime" gorm:"service_uptime;size:255;default:'';"`
		SystemTime        string `json:"system_time" gorm:"system_time;size:255;default:'';"`
		IPLocal           string `json:"ip_local" gorm:"column:ip_local;size:255;default:'';"`
		PortLocal         string `json:"port_local" gorm:"column:port_local;size:255;default:'';"`
	}
)

func GetAllHost() (Hosts, error) {
	hosts := make(Hosts, 0)
	if err := db.Model(&Hosts{}).Find(&hosts).Error; err != nil {
		return nil, err
	}
	return hosts, nil
}

func GetHosts(index, size int) ([]*Host, int, error) {
	// TODO: Improve the logging
	hosts := make([]*Host, 0)
	total := 0
	if err := db.Model(&Host{}).Count(&total).Error; err != nil {
		return nil, -1, err
	}

	if err := db.Model(&Host{}).Offset((index - 1) * size).Limit(size).Find(&hosts).Error; err != nil {
		return nil, -1, err
	}

	return hosts, total, nil
}

func GetHost(id int) (*Host, error) {
	host := new(Host)
	host.ID = uint(id)
	if err := db.First(host).Error; err != nil {
		logrus.Errorf("查找主机[ID: %d]失败[错误原因: %s]", id, err)
		return nil, fmt.Errorf("查询ID为%d的主机失败", id)
	}

	return host, nil
}

func AddHost(host *Host) error {
	// TODO: Improve the logging
	// 将项目信息落库
	if err := db.Create(host).Error; err != nil {
		return fmt.Errorf("添加主机失败，数据回滚失败")
	}

	return nil
}

func UpdateHost(id int, change *Host) error {
	// TODO: Improve the logging
	tx := db.Begin()
	defer tx.Commit()

	host, err := GetHost(id)
	if err != nil {
		return err
	}

	if err := tx.Model(host).Updates(change).Error; err != nil {
		tx.Rollback()
		return err
	}
	return nil

}

func (h *Host) Delete() error {
	// TODO: Improve the logging
	var err error

	tx := db.Begin()
	defer tx.Commit()

	if err = tx.Delete(h).Error; err != nil {
		goto RollBack
	}
	return nil
RollBack:
	tx.Rollback()
	return err
}
